using System;
using UnityEngine;

[Serializable]
public class sparks : MonoBehaviour
{
	[HideInInspector]
	public adefence basedefence;

	[HideInInspector]
	public adefence ringdefence;

	[HideInInspector]
	public adefence weightdefence;

	private bool vibrations;

	public ParticleSystem particles;

	public AudioSource aud;

	public AudioSource aud2;

	public bool player;

	public spinarcadeAI spinA;

	public AudioSource spinSound;

	private Rigidbody rigi;

	private bool grounded;

	private spin spin;

	private bool AI;

	private float lastCollision;

	private bool hammerTime;

	public sparks()
	{
		hammerTime = true;
	}

	public void Start()
	{
		if (!particles)
		{
			particles = GetComponentInChildren<ParticleSystem>();
		}
		rigi = GetComponent<Rigidbody>();
		if (tag == "AI")
		{
			AI = true;
			hammerTime = false;
		}
		else
		{
			AI = false;
			spin = (GetComponent("spin") as spin);
		}
		InvokeRepeating("UpdateSound", 0f, 0.05f);
	}

	public void OnLevelWasLoaded()
	{
		vibrations = (PlayerPrefs.GetInt("Vibration") == 1);
	}

	public void OnCollisionStay()
	{
		grounded = true;
		if (!spinSound.isPlaying)
		{
			spinSound.Play();
		}
		float sqrMagnitude = rigi.velocity.sqrMagnitude;
		if (!AI)
		{
			spinSound.volume = Mathf.Clamp(sqrMagnitude * 0.12f, 0f, 0.7f) + (float)spin.RPM * 0.00015f;
			spinSound.pitch = Mathf.Clamp(0.7f + sqrMagnitude * 0.006f + (float)spin.RPM * 0.0001f, 0.7f, 1.15f);
		}
		else
		{
			spinSound.volume = Mathf.Clamp(0.05f + sqrMagnitude * 0.13f, 0f, 0.6f);
			spinSound.pitch = Mathf.Clamp(0.7f + sqrMagnitude * 0.006f, 0.7f, 1.15f);
		}
	}

	public void UpdateSound()
	{
		if (hammerTime && StateManager.playing && !(Time.timeSinceLevelLoad - lastCollision < 60f))
		{
			JCAchievementMunger.UnlockAchievement("CgkI3_DHnsgeEAIQJA");
			hammerTime = false;
		}
		if (grounded)
		{
			if (!AI)
			{
				spinSound.volume = Mathf.SmoothStep(spinSound.volume, (float)spin.RPM * 0.0001f, 0.1f);
			}
			else
			{
				spinSound.volume = Mathf.SmoothStep(spinSound.volume, 0f, 0.1f);
			}
		}
	}

	public void OnCollisionExit()
	{
		grounded = false;
	}

	public void OnCollisionEnter(Collision collision)
	{
		if (!(collision.relativeVelocity.sqrMagnitude >= 1.5f))
		{
			return;
		}
		bool flag = collision.gameObject.tag == "Player";
		if (flag || collision.gameObject.tag == "AI")
		{
			aud.Play();
			part();
			if ((bool)basedefence)
			{
				basedefence.coll();
				ringdefence.coll();
				weightdefence.coll();
			}
			if (player)
			{
				if (vibrations)
				{
					vibrate();
				}
				lastCollision = Time.timeSinceLevelLoad;
			}
			if ((bool)spinA && flag)
			{
				spinA.spinAdd += 5;
			}
		}
		else if (!(collision.relativeVelocity.sqrMagnitude <= 20f))
		{
			aud2.Play();
		}
	}

	public void part()
	{
		particles.Play();
	}

	public void vibrate()
	{
		Handheld.Vibrate();
	}

	public void Main()
	{
	}
}
